<?php

// -----------------------------------------------------------------------------

$inputedUserName = false;
$inputedPassword = false;
$correctUserName = false;
$correctPassword = false;

if (isset($_COOKIE[$cookieUsername])) {
    $inputedUserName = true;

    if ($_COOKIE[$cookieUsername] == hash("sha256", $username)) {
        $correctUserName = true;
    }

}

if (isset($_COOKIE[$cookiePassword])) {
    $inputedPassword = true;

    if ($_COOKIE[$cookiePassword] == hash("sha256", $password)) {
        $correctPassword = true;
    }

}

if ($inputedUserName && $inputedPassword && $correctUserName && $correctPassword) {
    // do nothing;
} else {

    // 确保 $homepage 字符串结尾包含斜杠 "/"
    if ($homepage !== "/") {
        // 首先删除 $homepage 字符串结尾的 "/"，确保执行下一语句添加 "/" 后，结尾不会出现多个 "/"。
        $homepage = rtrim($homepage, "/");
        $homepage = $homepage . "/";
    }

    $str = "Location:" . $homepage . "login.php";
    header($str);

    exit;

}
